import math

'''
测试函数
'''
def GrieFunc(vardim, x, bound):
    """
    Griewangk function
    函数解释随着量变而改变，函数的真个数据分布中存在大量局部极值.。检测算法跳出局部的能力
    全局最小值 f(0) = 0
    """
    s1 = 0.
    s2 = 1.
    for i in range(1, vardim + 1):
        s1 = s1 + x[i - 1] ** 2
        s2 = s2 * math.cos(x[i - 1] / math.sqrt(i))
    y = (1. / 4000.) * s1 - s2 + 1
    #y = 1. / (1. + y)
    return y


def RastFunc(vardim, x, bound):
    """
    Rastrigin function
    此函数是基于De Jong函数，增加了一个余弦调制传递函数来产生频繁的局部最小值
    特点： 极小值的位置是有规律的
    用来检测在解有规律的一种情况，算法的实用性
    """
    s = 0
    for i in range(vardim):
        s = s + ((x[i]) ** 2 - 10 * math.cos(2 * math.pi * (x[i]))+10)
    return s

def ACKLEY(vardim,x,bound):
    z = 0
    y = 0
    a = 20
    b = 0.2
    c = 2*math.pi

    for i in range(vardim):
        z = x[i]**2+z
        y = math.cos(c*x[i])+y

    f = -a*math.exp(-b*math.sqrt(z/(vardim+1))) - math.exp(y/(vardim+1)) + a + math.exp(1)
    return f


def Sphere(vardim,x,bound):
    s = 0
    for i in range(vardim):
        s = s + x[i]**2

    return s

def Rosenbrock(vardim,x,bound):
    s = 0
    for i in range(vardim-1):
        a = (x[i+1]-x[i]**2)**2
        b = (x[i] - 1)**2
        s = s+ 100*a + b

    return s